package com.zgs.mall.order.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zgs.mall.order.entity.OrderEntity;
import com.zgs.mall.order.service.OrderService;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.R;



/**
 * 订单
 *
 * @author zgs
 * @email 
 * @date 2020-07-12 14:55:26
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/seataTesta")
    public R seataTesta() throws ExecutionException, InterruptedException {
        orderService.seataTesta();
        return R.ok();
    }


    @RequestMapping("/status/{orderSn}")
    public R getOrderStatus(@PathVariable("orderSn")  String orderSn) {
            OrderEntity entity=orderService.getOrderStatus(orderSn);
        return R.ok().setData(entity);
    }

    @RequestMapping("/seandMessage")
    public R seandMessage(){
      for (long i=0;i<10;i++){
          if(i%2==0){
              OrderEntity OrderEntity  =new OrderEntity();
              OrderEntity.setId(i);
              OrderEntity.setBillContent("你好");
              rabbitTemplate.convertAndSend("hello-direct-exchange","hello.Queue",OrderEntity,new CorrelationData(UUID.randomUUID().toString()));
          }else{
              rabbitTemplate.convertAndSend("hello-direct-exchange","hello.Queue","哈哈哈哈哈");
          }
      }
        return R.ok();
    }

    @Autowired
    private OrderService orderService;
    @RequestMapping("/test")
    //@RequiresPermissions("order:order:list")
    public R test(){
      orderService.test();

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
